package redis

import (
	"context"
	"fmt"
	"time"
)

func NewSms() *sms {
	return &sms{}
}

var Sms = NewSms()

type sms struct{}

const smsExpTime = time.Minute * 5

func (s *sms) smsKey(phone string) string {
	return fmt.Sprintf("%v", phone)
}

// SetVerificationCode 设置验证码
func (s *sms) SetVerificationCode(ctx context.Context, phone, code string) error {
	return KV.AddValueWithExpireTime(ctx, fmt.Sprintf("%v:%v", prefix, "sms"), s.smsKey(phone), code, smsExpTime)
}

// CheckVerificationCode 检查验证码
func (s *sms) CheckVerificationCode(ctx context.Context, phone, code string) bool {
	var (
		insideCode string
	)
	return KV.GetValue(ctx, fmt.Sprintf("%v:%v", prefix, "sms"), s.smsKey(phone), &insideCode) == nil && insideCode == code
}

// DeleteVerificationCode 删除验证码
func (s *sms) DeleteVerificationCode(ctx context.Context, phone string) error {
	return KV.DelValue(ctx, fmt.Sprintf("%v:%v", prefix, "sms"), s.smsKey(phone))
}
